#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.

from operator import add
from functools import reduce
from math_tools import float_equal
from math import sqrt


# def stretch_vector(vector, coefficient):
def scale_vector(vector, coefficient):
    ''' 缩放向量
        k * v1
    '''
    l = lambda i : coefficient * i
    return list(map(l, vector))

# dot_cross_product 中已经有
# def normalize(vector):
#     square = lambda i : i * i
#     coefficient = sqrt(sum(map(square, vector)))
#     return scale_vector(vector, 1 / coefficient)

def add_vector(v1, v2):
    ''' v1 + v2 '''
    return map(add, v1, v2)

def linear_combination(vectors, coefficient_list):
    ''' 向量线性组合
        a*v1 + b*v2 + c*v3
    '''
    stretched_vectors = []

    for idx, v in enumerate(vectors):
        co = coefficient_list[idx]

        stretch = scale_vector(v, co)
        stretched_vectors.append(stretch)

    return list(reduce(add_vector, stretched_vectors))

def is_linear_independent(vectors):
    ''' vectors中向量是否线性独立'''

def is_linear_dependent(vectors):
    ''' vectors中向量是否线性相关'''
    return not is_linear_independent(vectors)

def is_zero_vector(v):
    # return all( map(float_equal, ) )
    for ele in v:
        if not float_equal(ele, 0, 0.0000001):
            return False

    return True

# def eliminate_vector(v1, v2, idx):
#     # 这样会有小数, 不太好,
#     k1, k2 = pivot_row[idx], cur_row[idx]
#     if type(k1) is int and  type(k2) is int:
#         pass
#     # print(pivot_row, cur_row, coefficient)
#     # self.matrix[row_idx] = list( linear_combination(
#     #     [pivot_row, cur_row],
#     #     [1, -coefficient]
#     # ))


#     # 如果是整数,则用公约数除去
